package com.heima.redisso.task;

import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.heima.redisso.task
 * @Author: wangjian
 * @CreateTime: 2022-07-03  14:49
 * @Description: TODO
 * @Version: 1.0
 */
public class TestTasks {

    @Autowired
    private RedissonClient redissonClient;

    public void handleMessage() throws InterruptedException {
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("test-dqueue");
        // 发送一条初始化的消息
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        while (true) {
            // 监听消息,如果没有监听到消息,会进入30秒的阻塞状态
            String result = blockingQueue.poll(30, TimeUnit.SECONDS);
            if (result != null && !result.equals("init")) {
                System.out.println("接收到消息: " + result + " " + new Date());
            }
        }
    }
}
